package chaoyue.study.lock;

import chaoyue.study.ThreadUtils;

import java.util.concurrent.locks.LockSupport;

/**
 * LockSupport的简单使用
 */
public class LockSupportTest {
    private static String message;

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " start...");
            LockSupport.park();// park就是从当前Thread对象中获取许可
            System.out.println(message);
            System.out.println(Thread.currentThread().getName() + " end...");
        });

        Thread thread2 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " start...");
            message = "some message...";
            LockSupport.unpark(thread1);// unpark相当于是给对应的Thread对象发放许可
            System.out.println(Thread.currentThread().getName() + " end...");
        });

        thread1.start();
        ThreadUtils.sleep();
        thread2.start();
    }
}
